import { getPreSignedDownloadUrlAPI } from '@/services/oss/preSigned'
import { getDictDataListAPI } from '@/services/dict'

/**
 * 并发获取字典数据 hook
 * @param args 字典类型
 * @example const dictDataSource = await useGetDictData('dictType1','dictType2','dictType3');
 */
export const useGetDictData = async (...args) => {
  const dict = {}

  const getDictByType = async (dictType) => {
    return await getDictDataListAPI(dictType)
  }

  const dictList = await Promise.all(...args.map((v) => getDictByType(v)))

  args.forEach((dictType, index) => {
    dict[dictType] = dictList[index].data
      .filter((v) => v.status === 0)
      .map((v) => ({
        id: v.id,
        label: v.dictLabel,
        value: v.dictValue,
        remark: v.remark,
      }))
  })
  return dict
}

/**
 * 并发获取文件下载路径 hook
 * @param paths 路径地址
 * @example const downloadUrlList = await useGetDownloadUrl('url1','url2','url3');
 */
export const useGetDownloadUrl = async (...paths) => {
  const getDownloadUrl = async (url) => {
    return (await getPreSignedDownloadUrlAPI(url)).data
  }

  const promiseDownloadUrlList = paths.map((v) => getDownloadUrl(v))

  return await Promise.all(promiseDownloadUrlList)
}
